package imooc_math.graph;

import java.util.Iterator;
import java.util.List;

//求无权图的连通分量
public class Components {
	Graph g;
	boolean[] visited;
	int ccount;//连通分量个数
	int[] id;
	public Components(Graph g){
		this.g = g;
		System.out.println(g.V());
		visited = new boolean[g.V()];	
		id = new int[g.V()];
		ccount = 0;
		for(int i=0;i<g.V();i++){
			visited[i] = false;
			id[i] = -1;
		}
		for(int i=0;i<g.V();i++){
			if(!visited[i]){
				dfs(i);
				ccount++;
			}
		}
	}
	public void dfs(int v){
		id[v] = ccount;
		visited[v] = true;
		for(int i:g.adj(v)){
			if(!visited[i]){
				dfs(i);
			}
		}
		/*Iterable iterable = g.adj(v);
		Iterator iterator = iterable.iterator();
		while(iterator.hasNext()){
			if(!visited[(int)iterator.next()]){
				dfs((int)iterator.next());
			}
		}*/		
	}
	public int count(){
		return ccount;
	}
	
}
